import java.awt.image.BandCombineOp;
import java.util.Arrays;
import java.util.Scanner;

public class test {
    //只出现一次的数字
    public static int singleNum(int[] array){
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }
    public static void main(String[] args) {
        int[] array = {1,1,2};
        int ret = singleNum(array);
        System.out.println(ret);
    }
    public static int[] func2(int[] array,int target){
        int[] tmp = {-1,-1};//数组中不可能存负数，为了可以看见数组中值的变化
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j <array.length ; j++) {
                if( array[i] + array[j] == target){
                    tmp[0] = i;
                    tmp[1] = j;
                }
            }
        }
        return tmp;
    }
    public static void main15(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = func2(array,4);//返回值类型为数组，用数组来接收
        System.out.println(Arrays.toString(ret));
     }
    public static void swap(int[] array,int i,int j){
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }
    public static void func(int[] array){
        int i = 0;
        int j = array.length-1;
        while (i < j){
            while (i < j && array[i] % 2 != 0){
                i++;
            }while (i < j && array[j] % 2 == 0){
                j--;
            }
            swap(array,i,j);
        }
    }
    //实现数组中奇数在前，偶数在后
    public static void main14(String[] args) {
        int[] array = {1,4,5,3,2};
        func(array);
        System.out.println(Arrays.toString(array));
    }
    public static void second(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }
    public static int[] second2(int[] array){   //该方法不会改变原数组的值
        int[] tmp = new int[array.length];
        for (int i = 0; i < tmp.length ; i++) {
            tmp[i] = array[i] * 2;
        }
        return tmp;
    }
    public static void main13(String[] args) {
        int[] array = {1,4,3,2,5};
        /*second(array);
        System.out.println(Arrays.toString(array));*/
        int[] ret = second2(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }
    public static void main12(String[] args) {  //判断两个数组是否一样
        int[] array ={1,2,3,4,5};
        int[] array2 = {1,2,3,4,5};
        boolean flg = Arrays.equals(array,array2);
        System.out.println(flg);
    }
    //数组逆序
    public static void main11(String[] args) {
        int[] array = {1,2,3,4,5};
        int left = 0;
        int right = array.length-1;
        while (left < right){
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
        System.out.println(Arrays.toString(array));
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;   //为了判断有没有发生交换，没有交换则数组已有序，跳出循环即可
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j + 1]){ //交换
                    int tmp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = tmp;
                    flg = true;
                }
            }
            if(flg == false){   //说明没有发生交换
                break;
            }
        }
    }
    //二分查找,数组必须是有序的
    public static int myBinarySearch(int[] array,int key){
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = (right + left) / 2;
            if (array[mid] == key) {
                return mid;
            } else if (array[mid] < key) {
                left = mid + 1;
            } else  {
                right = mid - 1;
            }
        }
        return -1;
    }
    public static void main10(String[] args) {
        int[] array = {1,2,15,20,3,6};
        //bubbleSort(array);
        //Arrays.sort(array);   //直接调用方法（整体排）
        Arrays.sort(array,3,6);//直接调用方法（局部排）
        System.out.println("排序后的数组是"+Arrays.toString(array));
        //int n = myBinarySearch(array,6);
        int n = Arrays.binarySearch(array,6);  //同样可以调用方法进二分查找，或者局部查找
        if(n == -1){
            System.out.println("没有找到你要找的值；");
        }else {
            System.out.println("找到了你要找的值,下标是："+n);
        }
    }
    //顺序查找,效率太低
    public static int find(int[] array, int key){
        for (int i = 0; i < array.length ; i++) {
            if (array[i] == key) {
                return 1;
            }
        }
        return -1;
    }
    public static void main9(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int n = find(array,0);
        if(n == 1){
            System.out.println("找到了你要找的值；");
        }else {
            System.out.println("没有找到你要找的值；");
        }
    }
    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] copy = Arrays.copyOfRange(array,0,3);  //只拷贝某个范围的数组[0.3)
        System.out.println(Arrays.toString(copy));
    }
    public static void main6(String[] args) {
        int[] array = {2,3,4,5,6};
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);//该方法为Arrays.copyOf()的底层
        for(int x : copy){    //x用来接收数组中的每一个元素
            System.out.println(x+" ");
        }
    }
    public static void main5(String[] args) {
        int[] array = {6, 6, 6, 6, 6, 6};
       int[] copy = Arrays.copyOf(array,array.length*2);//数组扩容操作
        for(int x : copy){    //x用来接收数组中的每一个元素
            System.out.println(x+" ");
        }
    }
    //注意！！！这种不叫数组拷贝
    public static void main4(String[] args) {
        int[] array = {6, 6, 6, 6, 6, 6};
        int[] array2 = array;    //array2这个引用指向了array这个引用所指向的对象
        for(int x : array) {    //x用来接收数组中的每一个元素
            System.out.println(x + " ");
        }
    }
    //数组拷贝
    public static void main3(String[] args) {
        int[] array = {6,6,6,6,6,6};
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length ; i++) {
            copy[i] = array[i];
        }
        for (int i = 0; i < copy.length ; i++) {
            System.out.println(copy[i]);
        }
    }
    public static void main2(String[] args) {
        int[] array2 = null;  //表示array这个引用不指向任何的对象
        System.out.println(array2[0]);//此时程序会报错；
    }
    //三种打印数组的方式
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
        System.out.println();
        System.out.println("=============");
        for(int x : array){    //x用来接收数组中的每一个元素
            System.out.println(x+" ");
        }
        System.out.println();
        System.out.println("=============");
        //String ret = Arrays.toString(array);//该方法返回值类型为String类型,返回字符串
        //System.out.println(ret);
        System.out.println(Arrays.toString(array));
        }
    }

